package com.jochen.core.screen
import com.artemis.*
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.*
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.input.GestureDetector
import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.utils.viewport.FillViewport
import com.jochen.core.assets.*
import com.jochen.core.system.*
import com.jochen.core.ui.onClick
import com.jochen.core.ui.realize.*
import com.jochen.core.world.*

class GameScreen(
    game: GdxGame,
    screenManager: ScreenManager
    ): BaseScreen(game,screenManager){
    lateinit var world: World
    lateinit var worldBounds: Rectangle
    private lateinit var buildingSelectUi: BuilingSelectUi
    private lateinit var cameraControl: GestureDetector
    private val camera = game.camera
    init{
        initUi()
        initWorld()
        camera.setToOrtho(false,screenWidth.toFloat(),screenHeight.toFloat())
    }
    
    fun initUi(){
        val skin = game.assetService.get(SkinAsset.GOLDEN)
        val menuAtlas = game.assetService.get(AtlasAsset.MENU);
        val menuButton = ImageButton(UiStyle.getImageButtonStyle(menuAtlas,"044"));
        
        val menuView =  GameMenuView(game,this);
        
        menuButton.onClick{
                pause()
                menuView.updateAction(true);
        }
        
        val table = Table(skin);
        table.setFillParent(true);
        table.row();
        table.top().right().add(menuButton).padRight(30f).padTop(50f);
        
        buildingSelectUi = BuilingSelectUi(game,this);
        
        stage.addActor(buildingSelectUi);
        stage.addActor(table);
        stage.addActor(menuView);
    }
    
    fun initWorld() {
        worldBounds = Rectangle(-1000f, -1000f, 2000f, 2000f)
        val config = WorldConfiguration().apply {
            register(buildingSelectUi)
            register(game)
            register(game.batch)
            register(game.shapeRenderer)
            register(camera)
            setSystem(Mapper)
            setSystem(RendererSystem())
            setSystem(CameraControlSystem(worldBounds))
            setSystem(MapObjectSystem())
            setSystem(ClickSystem())
            setSystem(BuildingSystem())
        }
    
        world = World(config)
        WorldGen.initWorld(game, world, worldBounds)
        cameraControl = GestureDetector(world.getSystem(CameraControlSystem::class.java))
    }

    override fun update(delta: Float){
        camera.update()
        world.apply{
            setDelta(delta)
            process()
        }
    }
    
    override fun enter(){
        addProcessor(cameraControl);
        addProcessor(world.getSystem(ClickSystem::class.java));
    }
    
    override fun exit(){
        removeProcessor(cameraControl)
        removeProcessor(world.getSystem(ClickSystem::class.java))
    }
    
    override fun viewSizeChange(){
        camera.setToOrtho(false,screenWidth.toFloat(),screenHeight.toFloat())
    }
    
}